package com.pi.shisan.service.audio;

import com.pi.shisan.service.util.ComponentManager;
import com.pi.shisan.util.FileUtils;

import java.io.File;
import java.util.function.Consumer;

public class MixPlayer extends AudioPlayer{

    private AudioPlayer audioPlayer;

    public MixPlayer(File audioFile, AudioEffectComponentsSupplier<ComponentManager.MusicEffectBaseComponent> musicEffectComponentsSupplier) throws Exception {
        this(audioFile,FileUtils.getFileNameSuffix(audioFile.getName()),musicEffectComponentsSupplier);
    }

    public MixPlayer(File audioFile, String suffix,  AudioEffectComponentsSupplier<ComponentManager.MusicEffectBaseComponent> musicEffectComponentsSupplier) throws Exception {
        super(audioFile, musicEffectComponentsSupplier);
        if (suffix != null && suffix.trim().length() > 0) {
            if (suffix.equalsIgnoreCase("mp3")) {
                audioPlayer = new Mp3Player(audioFile, musicEffectComponentsSupplier);
            } else if (suffix.equalsIgnoreCase("ogg")) {
                audioPlayer = new OggPlayer(audioFile, musicEffectComponentsSupplier);
            } else if (suffix.equalsIgnoreCase("wav")) {
                audioPlayer = new WavPlayer(audioFile, musicEffectComponentsSupplier);
            } else if (suffix.equalsIgnoreCase("flac")) {
                audioPlayer = new FlacPlayer(audioFile, musicEffectComponentsSupplier);
            }
        }
        if (audioPlayer == null) {
            throw new RuntimeException("不支持的文件类型");
        }
    }

    @Override
    public long getTotalMilliseconds() {
        return audioPlayer.getTotalMilliseconds();
    }

    @Override
    public void setTitle(String title) {
        audioPlayer.setTitle(title);
    }

    @Override
    public void setOnNormalFinisedListener(Runnable onNormalFinishedListener) {
        audioPlayer.setOnNormalFinisedListener(onNormalFinishedListener);
    }

    @Override
    public void setRepeatTimes(int repeatTimes) {
        audioPlayer.setRepeatTimes(repeatTimes);
    }

    @Override
    public boolean playOrResume() {
        return audioPlayer.playOrResume();
    }

    @Override
    public void interrupt() {
        audioPlayer.interrupt();
    }

    @Override
    public void destroy() {
        audioPlayer.destroy();
    }

    @Override
    protected void doDestroy() {
        throw new RuntimeException("此方法不应该被调用到");
    }

    @Override
    protected boolean doingPlay() throws Exception {
        throw new RuntimeException("此方法不应该被调用到");
    }

    @Override
    protected void reset() throws Exception {
        throw new RuntimeException("此方法不应该被调用到");
    }

    @Override
    public void setPlayBeforeVolume(Consumer<Integer> playBeforeVolume) {
        this.audioPlayer.setPlayBeforeVolume(playBeforeVolume);
    }

    @Override
    public void setVolume(Integer volume) {
        this.audioPlayer.setVolume(volume);
    }
}
